{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c6e20649",
   "metadata": {},
   "source": [
    "上节课回顾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d1722554",
   "metadata": {},
   "outputs": [],
   "source": [
    "name =  \"凪\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ce157a68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'凪'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.capitalize()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca94fcd0",
   "metadata": {},
   "source": [
    "* 2022/9/20\n",
    "* python课堂笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6a67c42",
   "metadata": {},
   "source": [
    "# 本周内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80fd8c4a",
   "metadata": {},
   "source": [
    "> 1. 有序的数据结构之列表\n",
    "\n",
    "\n",
    "> 2. 用户输入\n",
    "\n",
    "\n",
    "> 3. 关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0f18755",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_01_name = \"Marry\"\n",
    "\n",
    "# 思考：如果想一个变量存放一个班同学的名字，该怎么存？\n",
    "# 思考1：处理数据会不会比较麻烦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3d72a14f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marry,Alex,Jack,周晓明\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = \"Marry,Alex,Jack,周晓明\"\n",
    "print(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9a861aaa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考2: 处理数据会不会比较麻烦？\n",
    "网三_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b7751c36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考3：我想知道这个名单有多少个人名？\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "088a011c",
   "metadata": {},
   "source": [
    "## 1. 引入列表的概念\n",
    "\n",
    "* list might contain items of different types,but usually the items all have the same type\n",
    "\n",
    "> 注释：我们已经学习了数值和字符串，list可以放数值和字符，但是通常要么一起放数值，要么一起放字符\n",
    "\n",
    "> 总结一些通用的有序数据结构方法：\n",
    ">> 1. 取值：通过索引值取出对应的内容\n",
    ">> 2. 切片：通过索引区间取出对应的一些内容\n",
    ">> 3. in 可以查看成员对象是否存在\n",
    "\n",
    "> 列表的一些方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "06e3ee3e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d71eb8a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', 'Alex', 'Jack', '周晓明'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = [\"Marry\",\"Alex\",\"Jack\",\"周晓明\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a19465ec",
   "metadata": {},
   "source": [
    "###  1.1 列表的运算法则（operation）\n",
    "\n",
    "* 列表的运算法则不会改变列表本身的形态\n",
    "* <font color = \"red\">列表的本身不做修改</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "77031342",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  取出 网三_name_list 中第一位同学的名字\n",
    "网三_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2cefd897",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  查看 网三_name_list 有多少位同学\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "243fbc4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三： Marry\n",
      "网三： Alex\n",
      "网三： Jack\n",
      "网三： 周晓明\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三：\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "358e33ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in  可以查询数据是否存在\n",
    "\"Jack\" in 网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "52231997",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则（in +  切片  取值  len（s)  s.count)\n",
    "网三_name_list_转专业 = [\"xiaoming\",\"Lihua\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8b58fa6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明', 'xiaoming', 'Lihua']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list + 网三_name_list_转专业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8113705e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考： 一个list  可不可以出现  重复项？\n",
    "网一_age_list = [18,19,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5823ceab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 20, 19]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae9add84",
   "metadata": {},
   "source": [
    "### 2.2 列表的方法（Function）—— 对列表数据进行操作\n",
    "> 1. 增\n",
    "> 2. 删\n",
    "> 3. 改\n",
    "> 4. 查\n",
    "\n",
    "\n",
    "* 不要重复运行，否则会重复增加，删减，改变或查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e5f5440f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 20, 19]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6b4fe4df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d595e52a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# append 增加，加在队尾\n",
    "网三_name_list.append(\"张志豪\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d2f0bd1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明', '张志豪']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "609cef9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"王一鸣\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "410c6b17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '李华', 'Jack', '周晓明', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "3a9dde23",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  insert 插入，增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "a1eb9bec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "b0510a92",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.insert(1,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "1a73f223",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '李华', '李华', 'Jack', '周晓明', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "6c7e7b05",
   "metadata": {},
   "outputs": [],
   "source": [
    "# reomve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "f75a332d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "376836db",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.remove(\"Alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee7e81d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "8e951689",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '李华', '李华', 'Jack', '周晓明', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove  经常和in 一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"Alex\" in 网三_name_list:\n",
    "    网三_name_list.remove(\"Alex\")\n",
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48b28657",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "517459e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "e8c596a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '李华', '李华', 'Jack', '周晓明', '张志豪', '王一鸣']"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "7baf174c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'王一鸣'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "10e83853",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "663ea9b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '李华', 'Jack', '周晓明', '张志豪']"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "3bd30fe0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：请移除19的元素\n",
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "3e89eac8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "9f7a1624",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "40551351",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路1:remove(value),先决条件，知道值出现的次数\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "    age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "a2518923",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "1c90d713",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "81307caf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "2 19\n",
      "6 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2：pop(index),先决条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "08e570c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 课后练习：\n",
    "# 1. 课本p68和p81\n",
    "# 2. 剩下的reverse，extend，clear，copy，sbrt回去之后尝试练习\n",
    "# 3.总结列表的方法，用表格的形式写在markdown上面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef57bb3d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
